<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>xiaxuchen的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="springmvc文件的上传下载基础知识获取resources目录下的文件，如 12val filename &#x3D; &quot;知题网-试题模版.xls&quot;val cpr &#x3D; ClassPathResource(&quot;excel&quot; + File.separator + filename)  浏览器弹出下载文件框  Content-disposition 是 MIME 协议的扩">
<meta property="og:type" content="article">
<meta property="og:title" content="xiaxuchen的博客">
<meta property="og:url" content="http://yoursite.com/2020/08/07/ssm/springmvc%E6%96%87%E4%BB%B6%E7%9A%84%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD/index.html">
<meta property="og:site_name" content="xiaxuchen的博客">
<meta property="og:description" content="springmvc文件的上传下载基础知识获取resources目录下的文件，如 12val filename &#x3D; &quot;知题网-试题模版.xls&quot;val cpr &#x3D; ClassPathResource(&quot;excel&quot; + File.separator + filename)  浏览器弹出下载文件框  Content-disposition 是 MIME 协议的扩">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="c:/Users/xxc、/AppData/Roaming/Typora/typora-user-images/image-20191207222644153.png">
<meta property="article:published_time" content="2020-08-07T15:08:01.929Z">
<meta property="article:modified_time" content="2020-07-19T10:36:53.787Z">
<meta property="article:author" content="xiaxuchen">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="c:/Users/xxc、/AppData/Roaming/Typora/typora-user-images/image-20191207222644153.png">
  
    <link rel="alternate" href="./atom.xml" title="xiaxuchen的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="./css/style.css">

<meta name="generator" content="Hexo 5.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="./" id="logo">xiaxuchen的博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="./">Home</a>
        
          <a class="main-nav-link" href="./archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="./atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-ssm/springmvc文件的上传下载" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="./2020/08/07/ssm/springmvc%E6%96%87%E4%BB%B6%E7%9A%84%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD/" class="article-date">
  <time datetime="2020-08-07T15:08:01.929Z" itemprop="datePublished">2020-08-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="springmvc文件的上传下载"><a href="#springmvc文件的上传下载" class="headerlink" title="springmvc文件的上传下载"></a>springmvc文件的上传下载</h1><h2 id="基础知识"><a href="#基础知识" class="headerlink" title="基础知识"></a>基础知识</h2><p>获取resources目录下的文件，如<img src="C:\Users\xxc、\AppData\Roaming\Typora\typora-user-images\image-20191207222644153.png" alt="image-20191207222644153"></p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">val</span> filename = <span class="string">&quot;知题网-试题模版.xls&quot;</span></span><br><span class="line"><span class="keyword">val</span> cpr = ClassPathResource(<span class="string">&quot;excel&quot;</span> + File.separator + filename)</span><br></pre></td></tr></table></figure>

<p>浏览器弹出下载文件框</p>
<blockquote>
<p>Content-disposition 是 MIME 协议的扩展，MIME 协议指示 MIME 用户代理如何显示附加的文件。Content-disposition其实可以控制用户请求所得的内容存为一个文件的时候提供一个默认的文件名，文件直接在浏览器上显示或者在访问时弹出文件下载对话框。<br> <strong>格式说明：</strong> content-disposition = “Content-Disposition” “:” disposition-type *( “;” disposition-parm ) 　</p>
</blockquote>
<p>解决文件名中文乱码问题</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> filenameEncoder: String</span><br><span class="line">    <span class="keyword">when</span> &#123;</span><br><span class="line">        agent.contains(<span class="string">&quot;MSIE&quot;</span>) -&gt; &#123;</span><br><span class="line">            <span class="comment">// IE浏览器</span></span><br><span class="line">            filenameEncoder = URLEncoder.encode(filename, <span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">            filenameEncoder = filenameEncoder.replace(<span class="string">&quot;+&quot;</span>, <span class="string">&quot; &quot;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        agent.contains(<span class="string">&quot;Firefox&quot;</span>) -&gt; &#123;</span><br><span class="line">            <span class="comment">// 火狐浏览器</span></span><br><span class="line">            <span class="keyword">val</span> base64Encoder = BASE64Encoder()</span><br><span class="line">            filenameEncoder = <span class="string">&quot;=?utf-8?B?&quot;</span> + base64Encoder.encode(filename.toByteArray(Charset.forName(<span class="string">&quot;utf-8&quot;</span>))) + <span class="string">&quot;?=&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> -&gt; <span class="comment">// 其它浏览器</span></span><br><span class="line">            filenameEncoder = URLEncoder.encode(filename, <span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>



<h2 id="下载文件"><a href="#下载文件" class="headerlink" title="下载文件"></a>下载文件</h2><p>响应ResponseEntity实现下载文件:</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping(<span class="meta-string">&quot;/questions/template&quot;</span>)</span></span><br><span class="line">  <span class="function"><span class="keyword">fun</span> <span class="title">download</span><span class="params">(<span class="meta">@RequestHeader(<span class="meta-string">&quot;User-Agent&quot;</span>)</span> agent:<span class="type">String</span>)</span></span>: ResponseEntity&lt;ByteArray&gt; &#123;</span><br><span class="line">      <span class="comment">// 获取目录下的资源</span></span><br><span class="line">      <span class="keyword">val</span> filename = <span class="string">&quot;知题网-试题模版.xls&quot;</span></span><br><span class="line">      <span class="keyword">val</span> cpr = ClassPathResource(<span class="string">&quot;excel&quot;</span> + File.separator + filename)</span><br><span class="line">      <span class="comment">// 获得请求头中的User-Agent</span></span><br><span class="line">      <span class="comment">// 根据不同的客户端进行不同的编码</span></span><br><span class="line">      <span class="keyword">var</span> filenameEncoder: String</span><br><span class="line">      <span class="keyword">when</span> &#123;</span><br><span class="line">          agent.contains(<span class="string">&quot;MSIE&quot;</span>) -&gt; &#123;</span><br><span class="line">              <span class="comment">// IE浏览器</span></span><br><span class="line">              filenameEncoder = URLEncoder.encode(filename, <span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">              filenameEncoder = filenameEncoder.replace(<span class="string">&quot;+&quot;</span>, <span class="string">&quot; &quot;</span>)</span><br><span class="line">          &#125;</span><br><span class="line">          agent.contains(<span class="string">&quot;Firefox&quot;</span>) -&gt; &#123;</span><br><span class="line">              <span class="comment">// 火狐浏览器</span></span><br><span class="line">              <span class="keyword">val</span> base64Encoder = BASE64Encoder()</span><br><span class="line">              filenameEncoder = <span class="string">&quot;=?utf-8?B?&quot;</span> + base64Encoder.encode(filename.toByteArray(Charset.forName(<span class="string">&quot;utf-8&quot;</span>))) + <span class="string">&quot;?=&quot;</span></span><br><span class="line">          &#125;</span><br><span class="line">          <span class="keyword">else</span> -&gt; <span class="comment">// 其它浏览器</span></span><br><span class="line">              filenameEncoder = URLEncoder.encode(filename, <span class="string">&quot;utf-8&quot;</span>)</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 模板代码，装配ResponseEntity</span></span><br><span class="line">      <span class="keyword">val</span> input = cpr.inputStream</span><br><span class="line">      <span class="keyword">val</span> body: ByteArray = ByteArray(input.available())</span><br><span class="line">      input.read(body)</span><br><span class="line">      <span class="keyword">val</span> headers = HttpHeaders()</span><br><span class="line">      <span class="comment">// 让浏览器显示下载文件对话框</span></span><br><span class="line">      headers.add(<span class="string">&quot;Content-Disposition&quot;</span>, <span class="string">&quot;attchement;filename=<span class="variable">$filenameEncoder</span>&quot;</span>)</span><br><span class="line">      <span class="keyword">val</span> statusCode = HttpStatus.OK</span><br><span class="line">      <span class="keyword">return</span> ResponseEntity(body, headers, statusCode)</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/08/07/ssm/springmvc%E6%96%87%E4%BB%B6%E7%9A%84%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD/" data-id="ckdkdagts0006vout0zg312e7" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="./2020/08/07/ssm/ssm%E4%B8%ADdao%E5%B1%82%E6%8A%BD%E5%8F%96(%E4%B8%8D%E5%AE%8C%E6%95%B4%E7%89%88)/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="./2020/08/07/android/Window%E7%9A%84%E4%BD%BF%E7%94%A8/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="./archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E9%93%BE%E8%A1%A8/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%95%B0%E7%BB%84/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/SpringBoot%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 xiaxuchen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="./" class="mobile-nav-link">Home</a>
  
    <a href="./archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="./fancybox/jquery.fancybox.css">

  
<script src="./fancybox/jquery.fancybox.pack.js"></script>




<script src="./js/script.js"></script>




  </div>
</body>
</html>